﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TableTool.Model;

namespace TableTool
{
    public partial class ucLook : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public LookModel mode { get; set; }

        public event Action<LookModel> Click;

        public DateTime LoadTime { get; set; }

        public string keystr {  get; set; }
        int exflag = 0;
        public ucLook()
        {
            InitializeComponent();
            this.Load += UcLook_Load;
            this.label1.Click += Label1_Click;
            LoadTime=DateTime.Now;
        }
        public void Ex()
        {
            if (mode==null)
            {
                return;
            }
            var sp = DateTime.Now - LoadTime;
            string addstr = exflag == 0 ? "/" : "*";
            if (exflag==0)
            {
                exflag = 1;
            }
            else
            {
                exflag = 0;
            }
            if (sp > TimeSpan.FromSeconds(mode.timeSecond))
            {
               var st=  string.Format("{0} cd:{1}s {2}", mode.Name, mode.timeSecond, "就绪");
                st += addstr;
                SetLable(label1,Color.Green, st);
            }
            else
            {
                var st = string.Format("{0} cd:{1}s {2}", mode.Name, mode.timeSecond, (mode.timeSecond - sp.Seconds).ToString());
                st += addstr;
                SetLable(label1,Color.Red, st);
            }
        }
        public bool To0(string downKeys)
        {
            if (downKeys.Contains(keystr))
            {
                LoadTime = DateTime.Now;
                return true;
            }
            return false;
        }
        private void SetLable(Label lb, Color value,string str)
        {
            if (lb.InvokeRequired)
            {
                lb.Invoke(new Action<Label, Color,string>(SetLable), lb, value, str);
            }
            else
            {
                lb.BackColor = value;
                lb.Text= str;
            }
        }
        private void Label1_Click(object? sender, EventArgs e)
        {
            Click?.Invoke(mode);
        }

        private void UcLook_Load(object? sender, EventArgs e)
        {
            if (mode != null)
            {
                label1.Text = string.Format("{0} cd:{1}s", mode.Name, mode.timeSecond);
                mode.Keys.ForEach(key => {
                    keystr += key.ToString() + " ";
                });
            }
         
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }

        }
    }
}
